package com.itwn.portal.web;

import com.github.pagehelper.PageInfo;
import com.itwn.portal.entity.Remark;
import com.itwn.portal.reult.ResponseResult;
import com.itwn.portal.service.RemarkService;
import com.itwn.portal.vo.RemarkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author cx
 * @date 2022/3/1
 * @apiNote
 */
@Controller
@RequestMapping("remark")
public class RemarkController {
    @Autowired
    RemarkService remarkService;

    @GetMapping("/list")
    @ResponseBody
    public ResponseResult<PageInfo<RemarkVo>> listByPage(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                         @RequestParam(value="size",defaultValue = "2") Integer size,
                                                         @RequestParam(value="pid",defaultValue = "0") Integer parentId, Integer rid,String comicName, String username){
        ResponseResult<PageInfo<RemarkVo>> result = new ResponseResult<>();
        try{
            System.out.println(parentId+":"+comicName+":"+username+':'+rid);
            PageInfo<RemarkVo> pageInfo = this.remarkService.queryByPage(now,size,parentId,comicName,username,rid);
            result.setCode(200);
            result.setMsg("查询评论成功!");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询评论失败!");
        }
        return result;
    }

    @RequestMapping("/listFirst")
    @ResponseBody
    public ResponseResult<PageInfo<RemarkVo>> listFirst(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                         @RequestParam(value="size",defaultValue = "2") Integer size,
                                                         Integer rid){
        ResponseResult<PageInfo<RemarkVo>> result = new ResponseResult<>();
        try{
            System.out.println(rid);
            PageInfo<RemarkVo> pageInfo = this.remarkService.queryFirstByPage(now,size,rid);
            result.setCode(200);
            result.setMsg("查询评论成功!");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询评论失败!");
        }
        return result;
    }
    @RequestMapping("/listHotFirst")
    @ResponseBody
    public ResponseResult<PageInfo<RemarkVo>> listHotFirst(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                         @RequestParam(value="size",defaultValue = "2") Integer size,
                                                         Integer rid){
        ResponseResult<PageInfo<RemarkVo>> result = new ResponseResult<>();
        try{
            System.out.println(rid);
            PageInfo<RemarkVo> pageInfo = this.remarkService.queryHotFirstByPage(now,size,rid);
            result.setCode(200);
            result.setMsg("查询评论成功!");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询评论失败!");
        }
        return result;
    }

    @GetMapping("/listSecond")
    @ResponseBody
    public List<RemarkVo> listSecond(Integer rid){
        return this.remarkService.querySecond(rid);
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseResult<Void> add(@RequestBody Remark remark){
        // 【！！！注意！！！】 异步给方法上加一个@ResponseBody即可
        //remark.setStatus("1"); 数据库给默认也可以
        remark.setStatus(0);
        remark.setRemarkTime(new Date());
        // likeCount和remarkCount也要设为0
        remark.setLikeCount(0);
        remark.setRemarkCount(0);
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            int i = this.remarkService.add(remark);
            if(i!=0){
                result.setCode(200);
                result.setMsg("保存成功!");
            }else{
                result.setCode(201);
                result.setMsg("保存失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result;
    }

    @GetMapping("/likeThat")
    @ResponseBody
    public ResponseResult<Void> likeThat(Integer mid){
        ResponseResult<Void> result = new ResponseResult<>();
        System.out.println("点赞评论中 :==============》" + mid);
        try{
            // 执行添加点赞数 service 接口
            this.remarkService.likeThat(mid);
            result.setCode(200);
            result.setMsg("点赞成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("点赞失败!");
        }
        return result;
    }

}
